
const chats = {
    state: {
        chats:[],
        messages:[],
    },
    mutations: {
      SET_CHATS: (state, chats) => {
        state.chats = chats
      },
      SET_MESSAGES: (state, messages) => {
        state.messages = messages
      },
    //   setSenderId
      SET_SENDERID: (state, data) => {
        state.chats.forEach(chat => {
          if(chat.chatId === data.chatId) {
            chat.messages.push(data.message)
          }
        })
      },
      SET_CHATSMESSAGE:(state, data)=>{
          state.chats.forEach(v=>{
            if(v.chatId===data.chatId){
              v.messages.push(data)
            }
          })
      }
    },
    actions: {
        initChats({ commit }, chats) {
            commit("SET_CHATS",chats)
        },
        setChatsMessage({ commit }, chats) {
            console.log(chats,222);
            commit("SET_CHATSMESSAGE",chats)
        },
        sender({ commit }, { chatId, message }){
            commit("SET_SENDERID",{chatId,message})
        }

    }
  }
  export default chats